<template>
  <div>
    <!-- 头部 -->
    <van-nav-bar
      title="注册"
      left-arrow
      fixed
      placeholder
      z-index="999"
      @click-left="$router.back()"
    />

    <!-- logo -->

    <div class="loginPage">
      <div class="logo">
        <img src="../assets/logo.jpg" alt="" />
      </div>
    </div>
    <!-- 表单 -->
    <!--  从vant组件库复制   -->

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="regRules.username"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="regRules.password"
      />
      <div style="margin: 16px">
        <van-button round block type="info" color="#DB281F" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { Form } from "vant";
import { Field } from "vant";
import regRules from "@/utils/validator/reg";
export default {
  name: "reg",
  components: {
    [Form.name]: Form,
    [Field.name]: Field,
  },
  data() {
    return {
      username: "",
      password: "",
      regRules,
    };
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
      //  用post去提交
      this.$http.post("/apis/user/pub/register", values).then((res) => {
        console.log(res);

        if (res.code == 0) {
          this.$toast("注册成功");
          
          let { token, tokenExpired, uid } = res;
          localStorage.setItem("xl-token", token);
          localStorage.setItem("xl-tokenExpired", tokenExpired);
          localStorage.setItem("xl-uid", uid);

          setTimeout((v) => {
            this.$router.go(-2);
          }, 800);
        } else {
          this.$toast(res.msg || "注册失败");
        }
      });
    },
  },
};
</script>

<style lang="less">
.loginPage {
  width: 100%;

  .logo {
    width: 180px;
    margin: 50px auto 0;
    img {
      width: 180px;
    }
  }
}
</style>